package com.whtq.front.common.aspect;

import com.alibaba.fastjson.JSONObject;
import com.whtq.front.common.model.response.WHTQResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * Created by
 *
 * @author wmi
 * @date 2021/9/14 - 11:45.
 * @description
 */
@Component
@Slf4j
public class ResponseResultInterceptor extends HandlerInterceptorAdapter {

    public static final String RESPONSE_RESULT = "RESPONSE-RESULT-WHTQ";

    private ThreadLocal<Long> timeThreadLocal = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 请求的方法
         */
        if (handler instanceof HandlerMethod) {
            final HandlerMethod handlerMethod = (HandlerMethod) handler;
            final Class<?> clazz = handlerMethod.getBeanType();
            final Method method = handlerMethod.getMethod();
            /**
             * 判断是否在类对象上面加了注解
             */
            if (clazz.isAnnotationPresent(WHTQResult.class)) {
                request.setAttribute(RESPONSE_RESULT, clazz.getAnnotation(WHTQResult.class));
            } else if (method.isAnnotationPresent(WHTQResult.class)) {
                request.setAttribute(RESPONSE_RESULT, method.getAnnotation(WHTQResult.class));
            }
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        log.info("<--------开始请求,请求方法:{}------------>", method.getName());
        long beginTime = System.currentTimeMillis();
        timeThreadLocal.set(beginTime);
        String requestURI = request.getRequestURI();
        log.info("访问的url-: " + requestURI);
        if (request.getParameterMap().size() > 0) {
            log.info("请求参数: " + JSONObject.toJSONString(request.getParameterMap()));
        } else {
            log.info("请求参数：无");
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            Description annotation = method.getAnnotation(Description.class);
            String description = "接口";
            if (null != annotation) {
                description = annotation.value();
            }
            long beginTime = timeThreadLocal.get();
            long endTime = System.currentTimeMillis();
            log.info("<-------结束请求,{} 请求耗时:{}毫秒------->", description,
                    endTime - beginTime);
        } else {
            log.info("handler" + handler.getClass().toString() + " is not HandlerMethod,");
        }

        super.afterCompletion(request, response, handler, ex);
    }
}
